﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Media.Imaging;

namespace VoiceMessager
{
	public static class Statics
	{
        public static Random Random { get; set; }
        public static string DefaultIp { get; set; }
        public static string DefaultMask { get; set; }
        public static string DefaultGateway { get; set; }
	    public static string RandomMac()
	    {
	        var bytes = new byte[6];
	        Random.NextBytes(bytes);
	        while (bytes[0] % 2 != 0)
	        {
	            Random.NextBytes(bytes);
	        }
	        return string.Join(":", bytes.Select(i => i.ToString("X2")));
	    }


        public static string MacRegex =
			"[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]";

		public static string IpRegex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
		                               + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
		                               + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
		                               + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";

	    public static string Hex2Regex = "[0-9A-F][0-9A-F]";


        public static BitmapImage ToBitmapImage(this Bitmap bitmap)
	    {
	        using (var memory = new MemoryStream())
	        {
	            bitmap.Save(memory, ImageFormat.Png);
	            memory.Position = 0;

	            var bitmapImage = new BitmapImage();
	            bitmapImage.BeginInit();
	            bitmapImage.StreamSource = memory;
	            bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
	            bitmapImage.EndInit();
	            return bitmapImage;
	        }
	    }

	    public static string HexFormating(byte[] bytes, int wrap = 2)
	    {

	        var x = Enumerable.Range(0, 32)
	            .Select(i => string.Join(" ", bytes.Skip(8 * i).Take(8).Select(j => j.ToString("X2"))));
	        var enumerable = x as string[] ?? x.ToArray();
	        var y = Enumerable.Range(0, enumerable.Length / wrap + 1)
                .Select(i => string.Join("\t", enumerable.Skip(i * wrap).Take(wrap)));
            return string.Join("\r\n", y);
	    }
	}
 
}
